define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Sellerpayment,$stateParams,GoDialog,GoPage) {
		
		$scope.order_id =$stateParams.order_id;//订单号
		
		$scope.$on('$ionicView.afterEnter', function() {
			GoPage.init_top($scope, 70, 0);
			$scope.init_data();
		}, false);
		
		$scope.init_data = function() {
			Sellerpayment.get_order($scope.order_id, function(data) {
				$scope.order = data;
//				console.log(data)
				$scope.showinvoicexia=false;  //是否显示展开图标
				//判断是否显示发票信息		
				if($scope.order.is_tax=="person"){  //只显示发表抬头 
					$scope.showinvoicexia=false;										
				}
				if($scope.order.is_tax=="vat"){ //显示全部
					$scope.showinvoicexia=true;										
				}
			});
		}
		
		//商品详情页
		$scope.onshow_productdetail = function(goods_id,product_id,type,act_id){
			if(type == 'newtest'){
				GoPage.go_states("newtest_detail", {
					"act_id": act_id,
					"goods_id": goods_id
				});
			}
			else if(type == 'newnotice'){
				GoPage.go_states("newnotice_detail", {
					"act_id": act_id,
					"goods_id": goods_id
				});
			}
			else if(type == 'newsend'){
				GoPage.go_states("newsend_detail", {
					"act_id": act_id,
					"goods_id": goods_id
				});
			}
			else if(type == 'groupbuy'){
				GoPage.go_states("groupbuy_goods", {
					"act_id": act_id,
					"goods_id": goods_id,
					"product_id": product_id
				});
			}
			else if(type == 'timedbuy'){
				GoPage.go_states("panicbuying_goods", {
					"act_id": act_id,
					"goods_id": goods_id,
					"product_id": product_id
				});
			}
			else if(type == 'spike'){
				GoPage.go_states("seckill_goods", {
					"act_id": act_id,
					"goods_id": goods_id,
					"product_id": product_id
				});
			}
			else if(type == 'gift') {
				GoPage.go_states("pointgoods", {
					"id": goods_id
				});
			} else{
				GoPage.go_states("productdetail", {
					"goods_id": goods_id,
					'type': "goods"
				});
			}
		}
		
		//控制发票信息显示
		$scope.showInvoiceInfo=function(){
			if($scope.showinvoicexia){
				$scope.invoiceflag=!$scope.invoiceflag;
			}
		}
		
		//处理订单-通用(个别按钮除外)
		$scope.handleOrder = function(method, order_id) {
			Sellerpayment.handleOrder(method, order_id, function(data) {
				if(data.success) {
					GoDialog.toast("操作成功！");
					$scope.init_data();
				} else {
					GoDialog.toast(data.msg);
				}
			})
		}
		//确认收款
		$scope.confirm = function(order_id, status) {
			Sellerpayment.confirm(order_id, status, function(data) {
				if(data.success) {
					GoDialog.toast(data.msg); 
					$scope.init_data();
				} else {
					GoDialog.toast(data.msg);
				}
			})
		}
		
		//根据按钮的类型，选择对应的事件
		$scope.switchEvent = function(buttonId) { //按钮id
			switch(buttonId) {
				case 1:
					{ //查看订单
						GoPage.go_states("sellerpayment", {"order_id": $scope.order.order_id});
						break;
					};
				case 2:
					{ //取消订单
						$scope.handleOrder("trades.order.cancel", $scope.order.order_id);
						break;
					};
				case 3:
					{ //确认收款
						$scope.confirm($scope.order.order_id);
						break;
					};
				case 4:
					{ //发货：从发货按钮跳转到sellerpayment，需要显示发货输入框
						GoPage.go_states("seller_send", {
							"order_id": $scope.order.order_id,
							"buttonId": buttonId
						});
						break;
					};
				case 5:
					{ //延长收货
						$scope.handleOrder("trades.order.extend", $scope.order.order_id);
						break;
					};
				case 6:
					{ //修改金额
						GoPage.go_states("afupdate", {
							"order_id": $scope.order.order_id
						});
						break;
					};
				case 7:
					{ //处理退款
						break;
					};
				case 8:
					{ //退货、退款
						GoPage.go_states("sellerrefund");
						break;
					};
				case 9:
					{ //收到退货
						break;
					};
				case 10:
					{ //举证
						break;
					};
				case 11:
					{ //上传凭证
						break;
					};
				case 14:
					{ //查看物流
						GoPage.go_states("orderlogistics",{'order_id':$scope.order.order_id});
						break;
					};		
			}
		}
		
		
	}

	controller.$inject = ['$scope', 'Sellerpayment','$stateParams','GoDialog','GoPage'];

	return app.registerController("sellerpaymentctrl",controller);
});